package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.web.util.CookieUtil;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Reference
    private IUserInfoService userInfoService;

    //测试
    @RequestMapping("/hello")
    @ResponseBody
    @RequireLogin
    public Object hello(Long id) {
        return userInfoService.get(id);
    }

    //手机验证
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone) {
        boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    //发送验证码
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.success();
    }

    //完成注册
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return AjaxResult.success();
    }

    //用户登录
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String username, String password, HttpServletResponse response){
        //查询账号密码是否存在
        String token = userInfoService.userLogin(username,password);

        //抽取工具类,把token对象设置到Cookie中,传输到浏览器
        //参数1:key :参数2:value 参数3:响应对象 参数4:存活时间
        CookieUtil.addCookie("token",token,response,RedisKeys.USER_LOGIN.getTime().intValue());

        return AjaxResult.success();
    }
}

